package com.study.bigwork.teacher.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.study.bigwork.entity.Teacher;
import com.study.bigwork.entity.User;
import com.study.bigwork.teacher.service.TeacherService;
import com.study.bigwork.user.service.UserService;
import com.study.bigwork.util.GetInfo;

import io.rong.models.response.TokenResult;

@Controller
@RequestMapping("teacher")
public class TeacherController {
	@Resource
	private TeacherService teacherService;
	@RequestMapping(value="/teacherLogin",method = RequestMethod.POST)
	public void updateUserInfo(HttpServletRequest request,HttpServletResponse response) throws IOException {
		// 设置编码方式
		request.setCharacterEncoding("utf-8");
		//设置返回数据格式和编码
        response.setContentType("application/json;charset=utf-8");
        
        //定义StringBuffer变量
      	StringBuffer stringBuffer = new StringBuffer();
        //line保存读取请求信息的当前一行，responseMessage为响应信息，返回信息
        String line = null, responseMessage = null;
        
        //输出流
        PrintWriter out = response.getWriter();
        
        //读取信息时会发生IO异常
        try{
            //BufferedReader为缓冲读取流
            BufferedReader bufferedReader = request.getReader();
            while((line = bufferedReader.readLine()) != null){
                stringBuffer.append(line);
            }
            System.out.println(stringBuffer);
            
        }catch(IOException e){
            e.printStackTrace();
        }
        
        //将json数据转为String
        Gson gson = new Gson();
        Teacher teacher =gson.fromJson(stringBuffer.toString(), Teacher.class);
        Teacher teacher2 = new Teacher();
        String sql;
     
        boolean b = false;
        boolean c = false;
        
        //调用TeacherService类中isExistUser方法访问数据库，并返回查询结果
        
        //使用手机号码+密码登录
        
        teacher2 = teacherService.findTeacherByPhonenumberAndPassword(teacher.getpNumber(),teacher.getPassword());
		       System.out.println(teacher2);
        responseMessage = gson.toJson(teacher2);
        System.out.println("teacher:对象转为json " + responseMessage);
        //输出流将信息返回
        out.print(responseMessage);
	}
	//获取chat信息
	@RequestMapping(value="/getChatInfo",method = RequestMethod.POST)
	public void getChatInfo(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		//设置返回数据格式和编码
        response.setContentType("application/json;charset=utf-8");
        
        //定义StringBuffer变量
      	StringBuffer stringBuffer = new StringBuffer();
        //line保存读取请求信息的当前一行，responseMessage为响应信息，返回信息
        String line = null, responseMessage = null;
        
        //输出流
        PrintWriter out = response.getWriter();
        
        //读取信息时会发生IO异常
        try{
            //BufferedReader为缓冲读取流
            BufferedReader bufferedReader = request.getReader();
            while((line = bufferedReader.readLine()) != null){
                stringBuffer.append(line);
            }
            System.out.println(stringBuffer);
            
        }catch(IOException e){
            e.printStackTrace();
        }
        
        //将json数据转为String
        Gson gson = new Gson();
        Teacher user = new Teacher();
        Teacher user1 =gson.fromJson(stringBuffer.toString(), Teacher.class);
        System.out.println("得到的参数chat_id："+user1.getChat_id());
        boolean b = false;
        
        //调用UserService类中isExistUser方法访问数据库，并返回查询结果
        user = this.teacherService.getChatInfo(user1);
        responseMessage = gson.toJson(user);
        System.out.println("对象转为json " + responseMessage);
        //输出流将信息返回
        out.print(responseMessage);
	}	
		
}
